document.addEventListener('DOMContentLoaded', function () {
    const carousel = document.querySelector('.carousel');
    const images = carousel.querySelectorAll('img');
    const prevButton = carousel.querySelector('.prev-btn');
    const nextButton = carousel.querySelector('.next-btn');
    const indicators = carousel.querySelector('.carousel-indicators');

    let currentIndex = 0;
    let slideInterval;

    // 创建指示器
    images.forEach((_, index) => {
        const indicator = document.createElement('span');
        if (index === 0) indicator.classList.add('active');
        indicator.addEventListener('click', () => {
            goToSlide(index);
        });
        indicators.appendChild(indicator);
    });

    const indicatorDots = indicators.querySelectorAll('span');

    // 显示当前幻灯片
    function showSlide(index) {
        // 隐藏所有图片
        images.forEach(img => {
            img.classList.remove('active');
        });

        // 移除所有指示器的活动状态
        indicatorDots.forEach(dot => {
            dot.classList.remove('active');
        });

        // 显示当前图片和指示器
        images[index].classList.add('active');
        indicatorDots[index].classList.add('active');
    }

    // 切换到指定幻灯片
    function goToSlide(index) {
        currentIndex = index;
        showSlide(currentIndex);
        resetInterval();
    }

    // 下一张幻灯片
    function nextSlide() {
        currentIndex = (currentIndex + 1) % images.length;
        showSlide(currentIndex);
    }

    // 上一张幻灯片
    function prevSlide() {
        currentIndex = (currentIndex - 1 + images.length) % images.length;
        showSlide(currentIndex);
    }

    // 重置自动轮播计时器
    function resetInterval() {
        clearInterval(slideInterval);
        slideInterval = setInterval(nextSlide, 3000);
    }

    // 绑定按钮事件
    prevButton.addEventListener('click', prevSlide);
    nextButton.addEventListener('click', nextSlide);

    // 鼠标悬停暂停轮播
    carousel.addEventListener('mouseenter', () => {
        clearInterval(slideInterval);
    });

    // 鼠标离开继续轮播
    carousel.addEventListener('mouseleave', resetInterval);

    // 初始化显示第一张幻灯片并开始自动轮播
    showSlide(0);
    resetInterval();
});